//
// Created by Administrator on 2025/8/8.
//
#include <iostream>
#ifndef STACK_H
#define STACK_H

namespace stack_array {

class stack {
public:
    explicit stack(const int capacity = 10) : capacity_(capacity), top(0) {
        data_  = new int[capacity];
    }
    ~stack() {
        delete[] data_;
    }
    void push(const int value) {
        //判断栈是不是满的
        if (isFull()) {
            //扩容
            expandSize(capacity_*2);
        }
        data_[top++] = value;
    }
    //栈是不是满的
    bool isFull()const {
        return top == capacity_;
    }
    //栈是不是空的
    bool isEmpty()const {
        return top == 0;
    }
    //扩容
    void expandSize(const int newCapacity) {
        const auto newCapacity_ = new int[newCapacity];
        memcpy(newCapacity_, data_, sizeof(int) * capacity_);
        delete[] data_;
        data_ = newCapacity_;
        capacity_ = newCapacity;
    }
    //获取栈顶元素
    int topValue()const {
        if (isEmpty()) {
            throw std::runtime_error("stack is empty");
        }
        return data_[top - 1];
    }
    //出栈
    void pop() {
        if (isEmpty()) {
            throw std::runtime_error("stack is empty");
        }
        top--;
    }
    //栈的大小
    int size()const {
        return top;
    }
    //栈的容量
    int capacity()const {
        return capacity_;
    }
    //打印栈
    void print()const {
        if (isEmpty()) {
            throw std::runtime_error("stack is empty");
        }
        for (int i = 0; i < top ;i++) {
            std::cout<<data_[i]<<" ";
        }
        std::cout<<std::endl;
    }

private:
    int* data_;//存放数据的地方
    int top;//栈顶的位置，也可以说是栈的大小
    int capacity_;//栈的容量
};

} // stack_

#endif //STACK_H
